import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_baidu_mapapi_map_example/CustomWidgets/map_appbar.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'
    show BMFMapSDK, BMF_COORD_TYPE;
import 'package:flutter_baidu_mapapi_map_example/loc.dart';
import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart';
import 'package:permission_handler/permission_handler.dart';
import 'flutter_map_demo.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 百度地图sdk初始化鉴权
  if (Platform.isIOS) {
    BMFMapSDK.setApiKeyAndCoordType(
        'cx0qYEESHHMqhhBKUpnBAMaqWWBgOOWn', BMF_COORD_TYPE.BD09LL);
    LocationFlutterPlugin.setApiKey("cx0qYEESHHMqhhBKUpnBAMaqWWBgOOWn");
  } else if (Platform.isAndroid) {
    // Android 目前不支持接口设置Apikey,
    // 请在主工程的Manifest文件里设置，详细配置方法请参考官网(https://lbsyun.baidu.com/)demo
    BMFMapSDK.setCoordType(BMF_COORD_TYPE.BD09LL);
  }
  Map map = await BMFMapAPI_Map.nativeMapVersion;
  print('获取原生地图版本号：$map');
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    _requestPermission(); // 执行权限请求
  }


  // 动态申请定位权限
  Future<bool> _requestPermission() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.location,
      Permission.storage,
    ].request();

    return statuses[Permission.location].isGranted &&
        statuses[Permission.storage].isGranted;


  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(builder: buildScaffold)
    );
  }

  ///创建一个widget
  Widget buildScaffold(BuildContext context) {
    return Scaffold(
      appBar: BMFAppBar(
        title: '百度地图flutter插件Demo',
        isBack: false,
      ),
      body:Column(children: [
        Container(
            child: FlatButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (_) {
                  return new LocationView();
                }));
//                  LocationFlutterPlugin().startLocation();
              },
              child: Text('定位'),
            )
        ),
        Expanded(child: FlutterBMFMapDemo()),
      ]),
    ); //Scaffo
  }
}
